package com.nanhai.buddhist.college.module.study.service.studytime;

import com.nanhai.buddhist.college.framework.common.util.collection.CollectionUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.nanhai.buddhist.college.module.study.controller.admin.studytime.vo.*;
import com.nanhai.buddhist.college.module.study.dal.dataobject.studytime.StudyTimeDO;
import com.nanhai.buddhist.college.framework.common.pojo.PageResult;

import com.nanhai.buddhist.college.module.study.convert.studytime.StudyTimeConvert;
import com.nanhai.buddhist.college.module.study.dal.mysql.studytime.StudyTimeMapper;

import static com.nanhai.buddhist.college.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.nanhai.buddhist.college.module.study.enums.ErrorCodeConstants.*;

/**
 * 教学计划时间 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class StudyTimeServiceImpl implements StudyTimeService {

    @Resource
    private StudyTimeMapper timeMapper;

    @Override
    public Long createTime(StudyTimeCreateReqVO createReqVO) {
        // 插入
        StudyTimeDO time = StudyTimeConvert.INSTANCE.convert(createReqVO);
        timeMapper.insert(time);
        // 返回
        return time.getId();
    }

    @Override
    public void updateTime(StudyTimeUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateTimeExists(updateReqVO.getId());
        // 更新
        StudyTimeDO updateObj = StudyTimeConvert.INSTANCE.convert(updateReqVO);
        timeMapper.updateById(updateObj);
    }

    @Override
    public void deleteTime(Long id) {
        // 校验存在
        this.validateTimeExists(id);
        // 删除
        timeMapper.deleteById(id);
    }

    private void validateTimeExists(Long id) {
        if (timeMapper.selectById(id) == null) {
            throw exception(TIME_NOT_EXISTS);
        }
    }

    @Override
    public StudyTimeDO getTime(Long id) {
        return timeMapper.selectById(id);
    }

    @Override
    public List<StudyTimeDO> getTimeList(Collection<Long> ids) {
        return CollectionUtils.isEmpty(ids) ? Collections.emptyList() : timeMapper.selectBatchIds(ids);
    }

    @Override
    public List<StudyTimeDO> getTimeAllList(StudyTimePageAllReqVO pageVO) {
        return timeMapper.selectList(pageVO);
    }

    @Override
    public PageResult<StudyTimeDO> getTimePage(StudyTimePageReqVO pageReqVO) {
        return timeMapper.selectPage(pageReqVO);
    }

    @Override
    public List<StudyTimeDO> getTimeList(StudyTimeExportReqVO exportReqVO) {
        return timeMapper.selectList(exportReqVO);
    }

}
